package com.example.mall.dao;

import com.example.mall.model.Account;
import com.example.mall.model.Belong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BelongDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Belong belong) {
        String sql = "insert into belong (tID, cID) values (?, ?)";
        jdbcTemplate.update(sql, belong.gettID(), belong.getcID());
    }

    public void delete(Integer id) {
        String sql = "delete from belong where id = ?";
        jdbcTemplate.update(sql, id);
    }

    public void update(Integer cID, Integer tID, Integer ntID) {
        String sql = "update belong set tID = ? where cID = ? and tID = ?";
        jdbcTemplate.update(sql, ntID, cID, tID);
    }

    public List<Belong> getByCId(Integer cID) {
        String sql = "select * from belong where cID = ? lock in share mode";
        RowMapper<Belong> rowMapper = new BeanPropertyRowMapper<>(Belong.class);
        return jdbcTemplate.query(sql, rowMapper, cID);
    }

}
